const path = require('path');
function resolve(dir) {
  return path.join(__dirname, dir);
}
const pages = {
  main: {
    entry: 'src/main.ts',
    template: 'src/index.html',
    filename: 'index.html'
  }
};

const cssLoaders = function (options) {
  options = options || {}

  const cssLoader = {
    loader: 'css-loader',
    options: {
      minimize: process.env.NODE_ENV === 'production',
      sourceMap: options.sourceMap
    }
  }

  var postcssLoader = {
    loader: 'postcss-loader',
    options: {
      sourceMap: options.sourceMap
    }
  }

  // generate loader string to be used with extract text plugin
  function generateLoaders (loader, loaderOptions) {
    const loaders = options.usePostCSS ? [cssLoader, postcssLoader] : [cssLoader]
    if (loader) {
      loaders.push({
        loader: loader + '-loader',
        options: Object.assign({}, loaderOptions, {
          sourceMap: options.sourceMap
        })
      })
    }

    // Extract CSS when that option is specified
    // (which is the case during production build)
    if (options.extract) {
      return ExtractTextPlugin.extract({
        use: loaders,
        fallback: 'vue-style-loader'
      })
    } else {
      return ['vue-style-loader'].concat(loaders)
    }
  }

  const stylusOptions = {
    'resolve url': true,
    // 这里 新增 import 配置项，指向自定义主题文件
    import: [path.resolve(__dirname, '../src/theme')]
  }

  return {
    css: generateLoaders(),
    postcss: generateLoaders(),
    less: generateLoaders('less'),
    sass: generateLoaders('sass', { indentedSyntax: true }),
    scss: generateLoaders('sass'),
    stylus: generateLoaders('stylus',stylusOptions),
    styl: generateLoaders('stylus',stylusOptions)
  }
}

//配置end
module.exports = {
  publicPath: "./",

  //禁用eslint
  lintOnSave: false,

  productionSourceMap: false,

  pages: {
    main: {
      entry: 'src/main.ts',
      template: 'src/index.html',
      filename: 'index.html'
    }
  },

  devServer: {
    index: 'index.html', //默认启动serve 打开index页面
    open: process.platform === 'darwin',
    host: '', // 127.0.0.1
    port: 5600,
    https: false,
    hotOnly: false,
    proxy: null, // 设置代理
    before: app => { }
  },

  // css相关配置
  css: {
    extract: true,
    sourceMap: false,
    loaderOptions: {
      less: {
        javascriptEnabled: true
      },
      stylus: {
        'resolve url': true,
        'import': [
          './src/theme'
        ]
      }
    },
    modules: false
  },

  // 是否为 Babel 或 TypeScript 使用 thread-loader。该选项在系统的 CPU 有多于一个内核时自动启用，仅作用于生产构建。
  parallel: require('os').cpus().length > 1,

  chainWebpack: config => {
    config.resolve.alias
      .set('@', resolve('src'));
  },

  pluginOptions: {
    'cube-ui': {
      postCompile: true,
      theme: true
    }
  }
}
